package com.mintuo.litecomposite.common.exception;

import com.mintuo.litecomposite.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;

/**
 * 异常处理器
 *
 * @Author scott
 * @Date 2019
 */
@RestControllerAdvice
@Slf4j
public class LiteCExceptionHandler {

    // 拦截：未登录异常
    @ExceptionHandler(NotLoginException.class)
    public ResultVo handlerException(NotLoginException e) {

        // 打印堆栈，以供调试
        e.printStackTrace();

        // 返回给前端
        return ResultVo.error(e.getMessage());
    }

    // 拦截：缺少权限异常
    @ExceptionHandler(NotPermissionException.class)
    public ResultVo handlerException(NotPermissionException e) {
        e.printStackTrace();
        return ResultVo.error("缺少权限：" + e.getPermission());
    }

    @ExceptionHandler(Exception.class)
    public ResultVo<?> handleException(Exception e){
        log.error(e.getMessage(), e);
        return ResultVo.error("操作失败，"+e.getMessage());
    }

}
